package script.esxi;

import cn.com.betasoft.dsat.probe.groovy.SimException;

/**
原生变量：g_col_pars g_col_other g_rets
原生变量：g_res(g_shc, g_dbcon)
样例
~ # df
Filesystem        Bytes         Used    Available Use% Mounted on
NFS                   0            0            0   0% /vmfs/volumes/NAS
VMFS-5     204547817472 178320834560  26226982912  87% /vmfs/volumes/datacluster
VMFS-5     494726545408 358820610048 135905935360  73% /vmfs/volumes/datastore130
vfat         4293591040     21299200   4272291840   0% /vmfs/volumes/4f446a44-da26a3e1-2d5d-00259001aba7
vfat          261853184    133627904    128225280  51% /vmfs/volumes/5193477a-6adb30b3-e8ce-8f7dcb0a734f
vfat          261853184    147202048    114651136  56% /vmfs/volumes/e940f776-1de39b61-bc44-fad8c5c6e179
vfat          299712512    184737792    114974720  62% /vmfs/volumes/4f446a2b-9d7d8e66-ae4c-00259001aba7

*/
def json = [];
//获取数据
def String ret = g_shc.collect('''df -m''');
if (ret.contains("not found")){
	throw new SimException(8016,"ESXi.OSFileSystem:没有找到命令.");
}
if (ret.contains("permission denied")){
	throw new SimException(8017,"ESXi.OSFileSystem:没有权限执行命令.");
}
def matched = false;
ret.split("\n").each{
	matched = true;
	def matcher = (it =~ /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+(\S+)/);
	if (matcher.matches()){
		def dfline =['FileSystem':'','Total':-1,'Used':-1,'Free':-1,'UsedRatio':-1,'MountedOn':'','InodeCount':-1,'InodeUsedRate':-1];
		def free = (int)(matcher[0][4].toLong());
		def used = (int)(matcher[0][3].toLong());
		def total = (int)(matcher[0][2].toLong());
		dfline['FileSystem'] = matcher[0][1];
		dfline['Total'] = total;
		dfline['Used'] = used;
		dfline['Free'] = free;
		dfline['UsedRatio'] = matcher[0][5];
		dfline['MountedOn'] = matcher[0][6];
		json.add(dfline);
	}
};
if(!matched){
	throw new SimException(8018,"ESXi.OSFileSystem:返回数据不匹配.");
}
//序列化返回
g_rets = json;